using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    class clsTHONG_SO_KI_THUAT
    {
        public clsTHONG_SO_KI_THUAT(string sConn)
	    {
		    SqlHelper.ConnectionString = sConn;
	    }
        public List<THONG_SO_KI_THUAT> DataTableToLstObj(DataTable dt)
        {
            THONG_SO_KI_THUAT tsktDTO;
            List<THONG_SO_KI_THUAT> lstTsktDTO = new List<THONG_SO_KI_THUAT>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                tsktDTO = new THONG_SO_KI_THUAT();
                try
                {
                    tsktDTO._maVTPT = dt.Rows[i]["MaVTPT"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    tsktDTO._tenTSKT = dt.Rows[i]["TenTSKT"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    tsktDTO._giaTriTSKT = dt.Rows[i]["GiaTriTSKT"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    tsktDTO._id = int.Parse(dt.Rows[i]["MaTSKT"].ToString());
                }
                catch (Exception ex)
                {
                }
                lstTsktDTO.Add(tsktDTO);
            }
            return lstTsktDTO;
        }
        public THONG_SO_KI_THUAT DataTableToObj(DataTable dt)
        {
            THONG_SO_KI_THUAT tsktDTO = new THONG_SO_KI_THUAT();
            try
            {
                tsktDTO._maVTPT = dt.Rows[0]["MaVTPT"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                tsktDTO._tenTSKT = dt.Rows[0]["TenTSKT"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                tsktDTO._giaTriTSKT = dt.Rows[0]["GiaTriTSKT"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                tsktDTO._id = int.Parse(dt.Rows[0]["MaTSKT"].ToString());
            }
            catch (Exception ex)
            {
            }
            return tsktDTO;
        }
        public List<THONG_SO_KI_THUAT> SelectTHONG_SO_KI_THUATByID(string id)
        {
            DataTable dt = new DataTable();
            List<THONG_SO_KI_THUAT> lstTsktDTO = new List<THONG_SO_KI_THUAT>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectTHONG_SO_KI_THUATByID";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaVTPT", id);
                //dt = SqlHelper.ExecuteToDataTable(spName, CommandType.Text);
                //lst = GetVYLUONG_KHOANLUONG(dt);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstTsktDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstTsktDTO;
        }
        public int InsertUpdateTHONG_SO_KI_THUAT(int? maTSKT, string tenTSKT, string giaTriTSKT, string maVTPT)
        {
            int result = 0;
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_InsertUpdateTHONG_SO_KI_THUAT";
                SqlParameter[] par = new SqlParameter[4];
                par[0] = new SqlParameter("@MaTSKT", maTSKT);
                par[1] = new SqlParameter("@TenTSKT", tenTSKT);
                par[2] = new SqlParameter("@GiaTriTSKT", giaTriTSKT);
                par[3] = new SqlParameter("@MaVTPT", maVTPT);
                result = SqlHelper.ExecuteNonQuery(spName, par);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return result;
        }
    }
}
